package store

import (
	"fmt"
	"sort"

	"file_explorer/common/consts"
	"file_explorer/common/util"
	"file_explorer/model"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/theme"
	"github.com/samber/lo"
)

type MetadataTabContext struct {
	DefaultTabContext

	Path       string
	FileInfos  []*model.ModifiedFileInfo
	CheckFiles []*model.ModifiedFileInfo
}

func (ctx *MetadataTabContext) GetTabType() string {
	return consts.TabTypeMetadata
}

func (ctx *MetadataTabContext) GetTabLabel() string {
	dirs := util.PathSplit2Nodes(ctx.Path)
	return fmt.Sprintf("%s: %s", ctx.GetTabType(), dirs[len(dirs)-1])
}

func (ctx *MetadataTabContext) GetTabIcon() fyne.Resource {
	return theme.DocumentCreateIcon()
}

// 文件列表排序
func (ctx *MetadataTabContext) SortFileInfos(cmp func(a, b *model.ModifiedFileInfo) bool) {
	sort.Slice(ctx.FileInfos, func(i, j int) bool {
		return cmp(ctx.FileInfos[i], ctx.FileInfos[j])
	})
}

func NewMetadataTabContext(fileTabContext *FileTabContext) *MetadataTabContext {
	if len(fileTabContext.CheckedList) == 0 {
		return nil
	}

	ctx := &MetadataTabContext{
		DefaultTabContext: newDefaultTabContext(fileTabContext.GetGlobalContext()),
		Path:              fileTabContext.GetPath(),
	}

	// 设置preview列表
	ctx.FileInfos = lo.Map(fileTabContext.CheckedList, func(item *model.FileInfo, _ int) *model.ModifiedFileInfo {
		return model.NewModifiedFileInfo(item)
	})

	return ctx
}
